package org.dtrd.modules.pushmessage.controller;


import org.dtrd.modules.pushmessage.entity.po.DtrdRlPushMessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.pushmessage.entity.bean.PushMessageDetail;
import org.dtrd.modules.pushmessage.entity.bean.PushMessageListInfo;
import org.dtrd.modules.pushmessage.entity.bean.PushMessageListRequest;
import org.dtrd.modules.pushmessage.entity.bean.PushMessagePageRequest;
import org.dtrd.modules.pushmessage.service.IDtrdRlPushMessageService;
import org.dtrd.common.api.vo.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import org.dtrd.base.BaseController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author xiao
 * @since 2022-08-08
 */
@Api(tags = "推送内容设置")
@RestController
@RequestMapping("/pushmessage/")
public class DtrdRlPushMessageController extends BaseController {

    @Autowired
    private IDtrdRlPushMessageService pushMessageService;

//    @ApiOperation(value = "查询详情", response = PushMessageDetail.class)
//    @GetMapping("")
//    @AutoLog(value="查询详情", operateType = OPERATE_TYPE_QUERY)
//    public Result<?> getPushMessage(@RequestParam Integer id){
//        PushMessageDetail info = pushMessageService.getPushMessage(id);
//        return Result.OK(info);
//    }

    @ApiOperation(value = "查询推送内容设置列表",response = PushMessageListInfo.class)
    @PostMapping("list")
    @AutoLog(value="查询推送内容设置列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getPushMessageList(@RequestBody PushMessageListRequest request){
        List<PushMessageListInfo> list = pushMessageService.getPushMessageList(request);
        return Result.OK(list);
    }

//    @ApiOperation(value = "分页查询列表", response = PushMessageListInfo.class)
//    @PostMapping("")
//    @AutoLog(value="查询列表", operateType = OPERATE_TYPE_QUERY)
//    public Result<?> getPushMessageList(@RequestBody PushMessagePageRequest request){
//        handlePageRequest(request);
//        IPage<PushMessageListInfo> page = pushMessageService.pagePushMessage(request);
//        return Result.OK(page);
//    }

    @ApiOperation(value = "新增或修改推送内容设置", notes = "一个机构只能应用一条推送内容")
    @PostMapping("edit")
    @AutoLog(value="新增或修改推送内容设置", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdatePushMessage(@RequestBody PushMessageDetail dto){
        // 如果应用状态不为空，且是状态为true，则是做应用状态修改，需要校验是否存在已经有应用的数据
        if(null != dto.getApplyStatus() && dto.getApplyStatus()){
            List<DtrdRlPushMessage> list = pushMessageService
                    .getPushMessageListByOrgCodeAndStatus(dto.getOrgCode(),dto.getApplyStatus());
            if (null != list && !list.isEmpty()){
                return Result.error("一个机构只能应用一条推送内容");
            }
        }
        boolean result = pushMessageService.saveOrUpdatePushMessage(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除推送内容设置")
    @DeleteMapping("delete")
    @AutoLog(value="删除推送内容设置", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removePushMessage(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = pushMessageService.removePushMessage(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
